<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <!-- class="mainor_tea" -->
            <el-button @click="export_de">导出</el-button>
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="采购方式" prop="mode">
              <el-select
                v-model.trim="form.mode"
                placeholder="采购方式"
                class="wid110"
                @change="changeMode"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(modeList, index) in list.modeList"
                  :key="index"
                  :label="modeList"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
              <el-select
                v-model.trim="form.buyer_id"
                placeholder="采购员"
                class="wid220"
                v-show="form.mode == 1"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(buyer, index) in list.buyer"
                  :key="index"
                  :label="buyer"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
              <el-select
                v-model.trim="form.supplier_id"
                placeholder="供应商"
                class="wid220"
                v-show="form.mode == 2"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(supplier, index) in list.supplier"
                  :key="index"
                  :label="supplier"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
              <el-select
                v-model.trim="form_mode_all"
                placeholder="所有"
                class="wid220"
                v-show="!form.mode"
              >
                <el-option label="所有" value=""></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="账期" prop="payment_days">
              <el-date-picker
                v-model="form.payment_days"
                type="month"
                placeholder="选择月"
                value-format="yyyy-MM"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="账单日期" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_num boxdq marginTop10">
          <div class="main_num_l box">
            <p>
              应付总额：
              <em>{{ list.money }}</em>
            </p>
            <p>
              已付总额：
              <em>{{ list.paid }}</em>
            </p>
            <p>
              抹零金额：
              <em>{{ list.zero_money_all }}</em>
            </p>
            <p>
              未付总额：
              <em>{{ list.unpaid }}</em>
            </p>
          </div>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
            :header-cell-class-name="this.$verticaline"
          >
            <el-table-column type="selection" />
            <el-table-column label="采购方式" >
              <template slot-scope="scope">{{
                list.modeList[scope.row.mode]
              }}</template>
            </el-table-column>
            <el-table-column label="采购员/供应商" >
              <template slot-scope="scope">{{
                scope.row.mode == 1
                  ? list.buyer[scope.row.buyer_id]
                  : list.supplier[scope.row.supplier_id]
              }}</template>
            </el-table-column>
            <el-table-column label="应付金额" prop="money" />
            <el-table-column label="已付金额" prop="paid" />
            <el-table-column label="抹零金额" prop="zero_money" />
            <el-table-column label="未付金额" prop="unpaid" />
            <el-table-column label="联系人" prop="name" />
            <el-table-column label="手机号" prop="mobile" />
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="140"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link type="success" @click="handleJump(scope.row)"
                  >结算</el-link
                >
                <!-- <router-link to="/financial/recharge">
                 
                </router-link> -->
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  account_generalLedger,
  export_account_info,
} from "@/api/financial/financialPre";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { getWeekStartAndEnd, parseTime } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 列表数据
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        start_time: "",
        end_time: "",
        mode: null,
        buyer_id: null,
        supplier_id: null,
        payment_days: parseTime(new Date(), "{y}-{m}"),
      },
      form_mode_all: "",
      createTime: [],
      pickerOptions: dateOption,
      selectArr: [],
    };
  },
  created() {
    // 列表
    this.account_generalLedger();
  },
  methods: {
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item, index) => {
        this.selectArr.push(item.index + 1);
      });
    },
    // 导出明细
    export_de() {
      let ids = "";
      if (this.selectArr.length > 0) {
        ids = this.selectArr.join("-");
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { ids: ids },
        this.list_form()
      );
      export_account_info(data)
        .then((res) => {
          const link = document.createElement("a");
          // let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          let blob = new Blob([res], {
            type: "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "采购总账";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 搜索
    getArticleList() {
      this.account_generalLedger();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.form.buyer_id = "";
      this.form.supplier_id = "";
      this.getArticleList();
    },
    // 采购方式切换
    changeMode() {
      this.form.buyer_id = null;
      this.form.supplier_id = null;
      this.form_mode_all = "";
    },
    // 列表
    account_generalLedger() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      account_generalLedger(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.forEach((e, index) => {
            e.index = index;
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";

      return form;
    },
    // 结算
    handleJump(row) {
      let mode = row.mode;
      let buyer_id = row.buyer_id;
      let supplier_id = row.supplier_id;
      let payment_days = this.form.payment_days;
      this.$router.push({
        name: "financial_recharge",
        params: {
          mode: mode,
          payment_days: payment_days,
          buyer_id: buyer_id,
          supplier_id: supplier_id,
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 120px);
  }
}
</style>
